package com.iqiyi.pps.epg.core.utils;

import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by karkarrotxu on 2014/12/4.
 */
public final class TransUtil {
    private TransUtil() {
    }

    public static List<PropertyFilter> builderFilterList(Map<String, String> params) {
        List<PropertyFilter> filterList = new ArrayList<PropertyFilter>();
        PropertyFilter filter = null;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            String filterName = entry.getKey();
            String value = entry.getValue();

            // 如果value值为空,则忽略此filter.
            if (StringUtils.isNotBlank(value)) {
                filter = new PropertyFilter(filterName, value);
                filterList.add(filter);
            }
        }
        return filterList;
    }

    ;

    public static List<PropertyFilter> builderFilterList(TFilter tFilter) {
        return builderFilterList(tFilter.getFilters());
    }

    // 构建分页对象
    public static Page buildPage(TPage tPage) {
        if (tPage == null) {
            return new Page(20); // 默认20
        } else {
            Page page = new Page();
            page.setAutoCount(tPage.isAutoCount());
            page.setPageNo(tPage.getPageNo());
            page.setPageSize(tPage.getPageSize());
            page.setOrder(tPage.getOrder());
            page.setOrderBy(tPage.getOrderBy());
            return page;
        }
    }

    public static String buildQueryStringByPropertyFilter(String hql, List<PropertyFilter> filters) {
        StringBuilder sb = new StringBuilder(hql);

        if (StringUtils.indexOfIgnoreCase(hql, "where") > 0) {
            sb.append(" and 1=1 ");
        } else {
            sb.append(" where 1=1 ");
        }

        for (PropertyFilter filter : filters) {
            if (!filter.hasMultiProperties()) { //只有一个属性需要比较的情况.
                sb.append(" and ");
                sb.append(filter.getPropertyName());
                sb.append(" ");
                makeUpFilter(sb, filter);
            } else {//包含多个属性需要比较的情况,进行or处理.
                sb.append(" and ( ");
                int i = 0;
                for (String param : filter.getPropertyNames()) {
                    if (i > 0) sb.append(" or ");
                    sb.append(param);
                    sb.append(" ");
                    makeUpFilter(sb, filter);
                    i++;
                }

                sb.append(" ) ");
            }
        }
        return sb.toString();
    }

    public static String makeUpFilter(StringBuilder sb, PropertyFilter filter) {
        if (PropertyFilter.MatchType.IN.equals(filter.getMatchType())) {
            List matchValue = (List) filter.getMatchValue();
            sb.append(" in ( ");
            int count = 0;
            for (Object single : matchValue) {
                if (count > 0) {
                    sb.append(",");
                }
                sb.append("'" + single + "'");
                count++;
            }
            sb.append(" ) ");
        } else {
            sb.append(PropertyFilter.getOperator(filter.getMatchType()));
            sb.append(" '");
            if (filter.getMatchType() == PropertyFilter.MatchType.LIKE) {
                sb.append("%" + filter.getMatchValue() + "%");
            } else {
                sb.append(filter.getMatchValue());
            }
            sb.append("' ");
        }
        return sb.toString();
    }

    public static String buildQueryStringByPropertyFilter(final Page page, String hql, final List<PropertyFilter> filters) {
        StringBuilder sb = new StringBuilder(hql);

        if (StringUtils.indexOfIgnoreCase(hql, "where") > 0) {
            sb.append(" and 1=1 ");
        } else {
            sb.append(" where 1=1 ");
        }

        for (PropertyFilter filter : filters) {
            if (!filter.hasMultiProperties()) { //只有一个属性需要比较的情况.
                sb.append(" and ");
                sb.append(filter.getPropertyName());
                sb.append(" ");
                makeUpFilter(sb, filter);
            } else {//包含多个属性需要比较的情况,进行or处理.
                sb.append(" and ( ");

                int i = 0;
                for (String param : filter.getPropertyNames()) {
                    if (i > 0) sb.append(" or ");
                    sb.append(param);
                    sb.append(" ");
                    makeUpFilter(sb, filter);
                    i++;
                }
                sb.append(" ) ");
            }
        }

        if (page == null) {
            return sb.toString();
        }
        //构造排序参数
        if (page.isOrderBySetted()) {
            String[] orderByArray = StringUtils.split(page.getOrderBy(), ',');
            String[] orderArray = StringUtils.split(page.getOrder(), ',');

            for (int i = 0; i < orderByArray.length; i++) {
                if (i == 0) {
                    sb.append(" order by ");
                } else {
                    sb.append(",");
                }

                sb.append(orderByArray[i]);
                sb.append(" ");
                if (Page.ASC.equals(orderArray[i])) {
                    sb.append(Page.ASC);
                } else {
                    sb.append(Page.DESC);
                }
            }
        }
        return sb.toString();
    }
}
